
define(function(require, exports, module) {
    var $=require('zepto');
    cn=require('conment');
    api=require('api');
    lg=require('login');
    pop=require('popup');
    require('../public/IdCardValidate.js');
    module.exports = {
        userInfo:JSON.parse(cn.getItem('user')),
        swipeTich:function(m){
            if(m.indexOf("工商")!=-1){
                return 'ICBC'
            }else if(m.indexOf("招商")!=-1){
                return 'CMB'
            }else if(m.indexOf("交通")!=-1){
                return 'COMM'
            }else if(m.indexOf("农业")!=-1){
                return 'ABC'
            }else if(m.indexOf("中国银行")!=-1){
                return 'BOC'
            }else if(m.indexOf("建设银行")!=-1){
                return 'CCB'
            }else if(m.indexOf("邮政")!=-1){
                return 'PSBC'
            }else if(m.indexOf("平安")!=-1){
                return 'SPABANK'
            }else if(m.indexOf("民生")!=-1){
                return 'CMBC'
            }else if(m.indexOf("光大")!=-1){
                return 'CEBBANK'
            }else if(m.indexOf("广发")!=-1){
                return 'GDB'
            }else if(m.indexOf("中信")!=-1){
                return 'CITIC'
            }else if(m.indexOf("兴业")!=-1){
                return 'CIB'
            }else if(m.indexOf("华夏")!=-1){
                return 'HXBANK'
            }else if(m.indexOf("交通")!=-1){
                return 'COMM'
            }else if(m.indexOf("浦发")!=-1){
                return 'SPDB'
            }else if(m.indexOf("北京")!=-1){
                return 'BJBANK'
            }else if(m.indexOf("上海")!=-1){
                return 'SHBANK'
            }else if(m.indexOf("光大")!=-1){
                return 'CEB'
            }else{
                return ''
            }
        },
        infoHtml:function () {
            var _this=this;

            if(_this.userInfo!=null&&_this.userInfo.name){
                $('.icon-real-info').addClass('active');
                $('.choose-line').css('width','100%');
                $('#safe-level').text('高');
                $('#no-verify').hide();

                $('#yes-verify').show().text(_this.userInfo.name);
                $('#real_name').show().text(_this.userInfo.name);
            }else{
                $('#no-verify').show()
                $('.verify').show();
            }
            $('#userName').text(_this.userInfo.username);
            $('#user_id').text(_this.userInfo.id);
            $('#user_phone').text(_this.userInfo.username);

        },
        /**
         * 实名认证
         * */

        secuurity:function () {
            var _this=this;
            $('.verify').on('click',function () {
                pop.popup_html({
                    id:'#popup-realname-auth',
                })
            });
            $('#real_name_card').on('click',function () {
                var cardID=$('#cardID').val().replace(/\s/g,""),cardName=encodeURI($('#realname').val().replace(/\s/g,""));
                if(cardName==''||cardName==null){
                    cn.alert('请输入身份证姓名');
                }else if(cardID==''||cardID==null){
                    cn.alert('请输入身份证号码');
                } else if(!IdCardValidate(cardID)){
                    cn.alert('身份证号码错误');
                }else{
                    _this.relname(cardID,cardName)
                }
            })
        },

        relname:function(ID,NM){
            var _this=this;
            cn.Ajax(api.certificate,{
                name:NM,
                idNum:ID,
            },function(d){
                if(d.flag==1){
                    cn.alert(d.message);
                    lg.isLogin(true)
                    setTimeout(function () {
                        location.reload()
                    },1000)
                }else {
                    cn.alert(d.message);
                }
            },'post')

        },
        /**
         * 实名认证 end
         * */

        /**
         * 绑定银行卡
         * */


        bindBank:function () {
            var _this=this;
            var name=$('#yes-verify').text();
            var kghang=$('#yhname').find("option:selected").text();
            var id=$('#yhname').val();
            var card_yh=$('#card_yh').val();
            var province=$('#province').find("option:selected").text();
            var zhihang=$('#zhihang').val();
            // console.log(kghang)
            if($('#yes-verify').is(":hidden")){
                cn.alert('请先实名认证')
            }else if(kghang=='请选择您的开户银行'){
                cn.alert('请选择您的开户银行')
            }else if(card_yh==''){
                cn.alert('请输入您的银行卡号')
            }else if(province=='请选择省份'){
                cn.alert('请选择开户支行')
            }else if(zhihang==''){
                cn.alert('请输入支行名称')
            }else{
                cn.Ajax(api.bankcardAdd,{
                    name:name,
                    bankcardNo:card_yh,
                    bankName:encodeURI(kghang),
                    bankcardBranch:encodeURI(zhihang),
                    hxBankId:id,
                },function(d){
                    if(d.flag==1){
                        cn.alert(d.message);
                        lg.isLogin(true)
                        setTimeout(function () {
                            location.reload()
                        },1000)
                    }else {
                        cn.alert(d.message)
                    }
                },'post')
            }
        },
        bank_list:function () {
            var _this=this;
            cn.Ajax(api.bankcardList,{
            },function(d){
                if(d.flag==1){
                    if(d.list.length>0){
                        $('#page_member_bankcard_no_card').hide().siblings('#page_member_bankcard_card').show();
                        $('#bank-card_own_img').attr('src','/img/bank/'+_this.swipeTich(d.list[0].bankName?d.list[0].bankName:'中国银行')+'@2x.png');
                        $('#user-bankNo').text((d.list[0].no).substring(0,4)+'**** **** '+(d.list[0].no).substring(d.list[0].no.length-4,d.list[0].no.length))
                        $('#user-bankNo2').text((d.list[0].no).substring(0,4)+'**** **** '+(d.list[0].no).substring(d.list[0].no.length-4,d.list[0].no.length))
                        $('#user_bankOn').text(d.list[0].name);
                        $('#bank_card_own').text(d.list[0].bankName);
                        $('#cardId').val(d.list[0].id)
                    }else{
                        $('#page_member_bankcard_card').hide().siblings('#page_member_bankcard_no_card').show();
                    }
                }
            },'post')
        },
        bindBankInit:function () {
            var _this=this;
            _this.bank_list();
            $('#submit-btn').on('click',function () {
                _this.bindBank();
            })
        },
        /**
         * end
         * **/


        /*
        *
        * 提现
        *
        * */

        applyCash:function () {
            var money=$.trim($("#extract_money").val())
            if(money==''||money==null){
                cn.alert('请输入要提现的金额');
                return false
            }else if(parseFloat(money)<=0){
                cn.alert('提现金额需大于0！');
                return false
            }else if(parseFloat(money)>parseFloat($('.money_txt').text())){
                cn.alert('超出最大可用金额');
                return false
            }
            cn.Ajax(api.applyCash,{
                amount:money,
                bankcardId:$('#cardId').val(),
            },function(d){
                if(d.flag==1){
                    pop.popup_success_msg({
                        text:d.message,
                        id:'#popup-p-error',
                        cBack:{
                            text:'确定',
                            callBack:function () {
                                window.location.href='/comview/balancehistory.html'
                                return false;
                            }
                        },
                    })
                }else{
                    cn.alert(d.message)
                }
            },'post')
        },

        extract_pay:function () {
            var _this=this;
            _this.bank_list();
            $('#mem_wdA').on('click',function () {
                _this.applyCash();
            })
        },
        /*
        * 提现 end
        * */

        init:function () {
            var _this=this;
            cn.Ajax(api.isLogin,{
            },function(d){
                if(d.flag==1){
                    cn.setItem('user',JSON.stringify(d));
                    _this.userInfo=d
                    cn.Ajax(api.finance,{
                    },function(d){
                        if(d.flag==1){
                            cn.setItem('moneyInfo',JSON.stringify(d));
                            _this.infoHtml();
                        }
                    },'post');

                }else{
                    localStorage.removeItem('moneyInfo');
                    localStorage.removeItem('user');
                    cn.deleteCookie('COOKIE_CUSER');
                    window.location.href='/comview/login.html'
                }
            },'post')


           if(!lg.userInfo()){
               window.location.href='/comview/login.html'
           }
        }
    }

});